<?php

class QuoteEmailsController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session');
  public $name = 'QuoteEmails';
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'QuoteEmail.email' => 'asc'
            ));
  }


//admin and public version
  function admin_index() {
    $this->layout = 'admin';
    $this->set('quoteEmails', $this->paginate('QuoteEmail'));
  }

  //admin
  function admin_add() {
    $this->layout = 'admin';
    $test = $this->QuoteEmail->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $data['QuoteEmail']['email']=$data['QuoteEmail']['email_email'];
      $this->QuoteEmail->set($data);
      if ($this->QuoteEmail->validates()) {
        if ($this->QuoteEmail->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          $data['QuoteEmail']['id']=$id;
        }
        $this->request->data['QuoteEmail'] = $data['QuoteEmail'];
        $this->set('errors', $this->QuoteEmail->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }
  
  function admin_delete($id) {
    //http://book.cakephp.org/2.0/en/models/deleting-data.html
    if ($this->QuoteEmail->delete($id, true)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

}

?>
